/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2022-12-08 09:38:59
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-08-23 16:50:27
 * @FilePath: \cxl-admin\src\apis\system\menu.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { storage } from '@/store/sessionStorage'
import axios from '../../axios/index'

/**
 * 菜单功能接口
 */
export interface MenuPermissionType {
    id?: string //菜单权限功能id
    name?: string //菜单权限名称
    url?: string //菜单权限后端接口
    code?: string //菜单权限前端code
    status?: number //状态(0.禁用；1.启用)
    menuId?: string //父级编码
}

/**
 * 新增菜单类型
 */
export type menuType = {
    code: string //菜单编码
    name: string //菜单名称
    icon?: string //菜单icon
    parentId?: string //父级编码
    sort?: number //排序
    status?: number //状态(0.禁用；1.启用)
    url?: string //前端路由地址
    component?: string //前端路由组件
    menuResourceDTOList?: MenuPermissionType[] //菜单功能列表
    id?: string //菜单id
    parentName?: string //父级菜单名称
}

/**
 * 分页查询菜单列表
 * @param current 当前分页
 * @param size   每页数量
 * @param name  菜单名称
 * @returns
 */
export const menuList = (name?: string) => {
    return axios({
        method: 'get',
        url: '/api/system/menu/list',
        params: { name }
    })
}
/**
 * 启用-禁用菜单
 * @param id 菜单id
 * @param status 菜单状态 1启用 0 禁用
 * @returns
 */
export const enAble = (id: string, status: number) => {
    return axios({
        method: 'post',
        url: '/api/system/menu/enable',
        data: { id, status }
    })
}

/**
 * 新增菜单
 */
export const menuAdd = (data: menuType) => {
    return axios({
        method: 'post',
        url: '/api/system/menu/add',
        data
    })
}

/**
 * 查询一级菜单
 * @returns
 */
export const getParentMenu = () => {
    return axios({
        method: 'get',
        url: '/api/system/menu/list-parent'
    })
}

/**
 * 查询对应的菜单功能 通过id
 * @param menuId
 * @returns
 */
export const getMenuFn = (menuId: string) => {
    return axios({
        method: 'get',
        url: '/api/system/menu/list-resource',
        params: { menuId }
    })
}

/**
 * 查询所有菜单的树形结构
 * @returns
 */
export const getAllMenuTree = () => {
    const str = storage.get('tree1') || '[]'
    const tree: any[] = [...JSON.parse(str)]
    return new Promise(resolve => {
        if (tree.length) {
            resolve(tree)
        } else {
            axios({
                method: 'get',
                url: '/api/system/menu/tree'
            }).then(res => {
                if (res.data.value === 200) {
                    resolve(res.data.data)
                    storage.set('tree', JSON.stringify(res.data.data))
                } else {
                    resolve([])
                }
            })
        }
    })
}

/**
 * 通过管理员ID查询tree
 * @param adminId
 * @returns
 */
export const getMenuTreeById = (adminId: string) => {
    return axios({
        method: 'get',
        url: '/api/system/menu/treeByAdminId',
        params: { adminId }
    })
}

/**
 * 通过id删除菜单
 * @param id
 * @returns
 */
export const menuDelete = (id: string) => {
    return axios({
        method: 'post',
        url: `/api/system/menu/delete/${id}`
    })
}

/**
 * 编辑菜单
 * @param data
 * @returns
 */
export const menuEdit = (data: menuType) => {
    return axios({
        method: 'post',
        url: '/api/system/menu/edit',
        data
    })
}

/**
 * 按钮权限编辑
 * @param data
 * @returns
 */
export const permissionEdit = (data: MenuPermissionType) => {
    return axios({
        method: 'post',
        url: '/api/system/resource/edit',
        data
    })
}
/**
 * 按钮权限新增
 * @param data
 * @returns
 */
export const permissionCreate = (data: MenuPermissionType) => {
    return axios({
        method: 'post',
        url: '/api/system/resource/add',
        data
    })
}

/**
 * 通过id删除按钮权限
 * @param id
 * @returns
 */
export const permissionDelete = (id: string) => {
    return axios({
        method: 'post',
        url: `/api/system/resource/delete/${id}`
    })
}

/**
 * 权限按钮 启用/禁用
 * @param id
 * @param status
 * @returns
 */
export const permissionEnable = (id: string, status: number) => {
    return axios({
        method: 'post',
        url: '/api/system/resource/enable',
        data: { id, status }
    })
}
